/*  MainGui.java
 * 
 *  WebCrawler Program
 *  @author Amandeep Jhajj and Craig Markham
 *  @date 26Apr2012
 */

package GUI;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Observable;
import java.util.Observer;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import Crawler.Controller;
import Crawler.ResultsString;
import Crawler.UI;
/**
 * the MainGui class
 *
 */
public class MainGui implements ActionListener, UI, Observer{
	final  boolean shouldFill = true;

	private  JFrame  my_frame = new JFrame("Web Crawler");
	private String my_url;
	private Set<String> words = new HashSet<String>();
	
	//TODO GUI field for num of threads
	private int my_threads_num = 10;
	private BufferedWriter myResultsFile;
	private BufferedWriter myLogFile;

	ResultGui result = new ResultGui();
	
	JTextField url_field = new JTextField("http://faculty.washington.edu/gmobus/");
	JTextField first_word_field = new JTextField("intelligence");
	JTextField second_word_field = new JTextField("artificial");
	JTextField third_word_field = new JTextField("agent");
	JTextField fourth_word_field = new JTextField("university");
	JTextField fifth_word_field = new JTextField("research");
	JTextField sixth_word_field = new JTextField("science");
	JTextField seventh_word_field = new JTextField("robot");
	JTextField eigth_word_field = new JTextField();
	JTextField ninth_word_field = new JTextField();
	JTextField tenth_word_field = new JTextField();
	JTextField number_of_threads = new JTextField("10");
	
	/**
	 * the action listener for the submit button
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		my_url = url_field.getText();
		words.add(first_word_field.getText());
		words.add(second_word_field.getText());
		words.add(third_word_field.getText());
		words.add(fourth_word_field.getText());
		words.add(fifth_word_field.getText());
		words.add(sixth_word_field.getText());
		words.add(seventh_word_field.getText());
		words.add(eigth_word_field.getText());
		words.add(ninth_word_field.getText());
		words.add(tenth_word_field.getText());
		
		my_threads_num = Integer.parseInt(number_of_threads.getText());

		final Controller controller = 
			new Controller(this, my_url, words, my_threads_num);
		controller.addObserver(this);
		controller.run();
		
    	String theResultsFileName = new String("spiderRun_" + my_threads_num + "_Threads.txt");
		try{
    	    myResultsFile = new BufferedWriter(new FileWriter(theResultsFileName));    	   
	    } catch (final Exception e) {
	      myResultsFile = null;
	    }
    
    	String theLogFileName = new String("log.txt");
		try{
    	    myLogFile = new BufferedWriter(new FileWriter(theLogFileName));    	   
	    } catch (final Exception e) {
	      myLogFile = null;
	    }
	    
		result.start();
		my_frame.dispose();
	}
	/**
	 * Updates ResultGUI with parsed page results and writes to file.
	 * @param theResultsString the results
	 */
	@Override
	public synchronized void reporterTriggered(ResultsString theResultsString) {
		
		final String myResultsString = theResultsString.getResultsString();
		
		result.update(myResultsString);
		try {
			if (myResultsFile != null) {
				myResultsFile.write(myResultsString);
				myResultsFile.write("\n");
			}
		} catch (IOException e) {
		}
		/* Failed attempt to include just a simple log - ran out of time
		try {
			if (myLogFile != null) {
				myLogFile.write(myLogString);
				myLogFile.write("\n");
			}
		} catch (IOException e) {
		}
		*/
	}
	
	
	/**
	 * the set up for the gui
	 * 
	 */
	public void setup(){
		//grid layout with 25 compents 
		my_frame.setLayout(new GridLayout(25, 1));	

		JLabel url  = new JLabel("URL:");
		my_frame.add(url);
		my_frame.add(url_field);
		
		JLabel first_word  = new JLabel("word 1:");
		my_frame.add(first_word);
		my_frame.add(first_word_field);
		
		JLabel second_word  = new JLabel("word 3:");
		my_frame.add(second_word);
		my_frame.add(second_word_field);
		
		JLabel third_word  = new JLabel("word 3:");
		my_frame.add(third_word);
		my_frame.add(third_word_field);
		
		JLabel fourth_word  = new JLabel("word 4:");
		my_frame.add(fourth_word);
		my_frame.add(fourth_word_field);
		
		JLabel fifth_word  = new JLabel("word 5:");
		my_frame.add(fifth_word);
		my_frame.add(fifth_word_field);
		
		JLabel sixth_word  = new JLabel("word 6:");
		my_frame.add(sixth_word);
		my_frame.add(sixth_word_field);
		
		JLabel seventh_word  = new JLabel("word 7:");
		my_frame.add(seventh_word);
		my_frame.add(seventh_word_field);
		
		JLabel eigth_word  = new JLabel("word 8:");
		my_frame.add(eigth_word);
		my_frame.add(eigth_word_field);
		
		JLabel ninth_word  = new JLabel("word 9:");
		my_frame.add(ninth_word);
		my_frame.add(ninth_word_field);
		
		JLabel tenth_word  = new JLabel("word 10:");
		my_frame.add(tenth_word);
		my_frame.add(tenth_word_field);
		
		JLabel number_treads = new JLabel("number of threads:");
		my_frame.add(number_treads);
		my_frame.add(number_of_threads);
		
		JButton submit_button = new JButton("Submit");
		my_frame.add(submit_button);
		
		my_frame.setSize(400, 700);
		my_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//my_frame.pack();
		my_frame.setVisible(true);
		
		
		submit_button.addActionListener(this);
	}
	@Override
	public void update(Observable arg0, Object arg1) {
		// TODO Auto-generated method stub
		
	}

}